package gui;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import br.ufpe.cin.miniJava.gui.Color;
import br.ufpe.cin.miniJava.gui.ComboBox;
import br.ufpe.cin.miniJava.gui.Panel;

public class ComboData extends Panel{
	protected ComboBox[] combo;
	
	private String[] dias;
	private String[] meses;
	private String[] anos;
	
	
	private Color corBranca;
	
	public ComboData(){
		this.setSize(135, 25);
		
		corBranca = new Color(253,253,253);
		
		combo = new ComboBox[3];
		
		dias = getStrings(31);
		
		meses = getStrings(12);
		
		anos = new String[101];
		
		for (int i = 0; i < 101 ; i++){
			anos[i] = "" + (i+2000);
		}

		combo[0] = addComboBox(dias,0,0,25,40);
		combo[0].addItemStateListener(new ComboDataListener(this));
		combo[1] = addComboBox(meses,40,0,25,40);
		combo[1].addItemStateListener(new ComboDataListener(this));
		combo[2] = addComboBox(anos,80,0,25,55);
		combo[2].addItemStateListener(new ComboDataListener(this));
		
		combo[0].setBackground(corBranca);
		combo[1].setBackground(corBranca);
		combo[2].setBackground(corBranca);
		
		GregorianCalendar calendario = new GregorianCalendar();
		
		int ano = calendario.get(Calendar.YEAR);
		int mes = calendario.get(Calendar.MONTH);
		int dia = calendario.get(Calendar.DAY_OF_MONTH);
		
		
		if(ano >= 2000 && ano < 2100){
			combo[2].setSelectedIndex(ano - 2000);
			combo[1].setSelectedIndex(mes);
			combo[0].setSelectedIndex(dia - 1);
		}
		
		
		
		
	}
	
	protected ComboBox addComboBox(String[] conjuntoDeStrings, int posX, int posY,int altura, int largura){
		ComboBox comboAdd;
		comboAdd = new ComboBox(conjuntoDeStrings);
		this.add(comboAdd,posX,posY);
		comboAdd.setSize(largura,altura);
		comboAdd.setBackground(corBranca);
		return comboAdd;
	}
	
	
	public String[] getStrings(int qtd){
		String[] str;
		str = new String[qtd];
		
		for (int i = 0; i < qtd ; i++){
			if(i < 9) str[i] = "0" + (i+1);
			else str[i] = "" + (i+1);
		}
		
		return str;
	}
	
	
	public int getDia(){
		return combo[0].getSelectedIndex()+1; 
	}
	
	public int getMes(){
		return combo[1].getSelectedIndex()+1; 
	}
	
	public int getAno(){
		return combo[2].getSelectedIndex()+2000; 
	}
	
	public String getData(){
		String data;
		
		String dia;
		String mes;
		String ano;
		
		
		int posDia = combo[0].getSelectedIndex();;
		int posMes = combo[1].getSelectedIndex();;
		int posAno = combo[2].getSelectedIndex();;
		
		if (posDia < 9) dia = "0" + (posDia+1);
		else dia = "" + (posDia+1);
		
		if (posMes < 9) mes = "0" + (posMes+1);
		else mes = "" + (posMes+1);
		
		ano = "" + (posAno+2000);
		
		data = dia + "/" + mes + "/" + ano;
		
		return data;
	}
	
	
	
	
	
		
}
